package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.CaseDataStatus;
import cn.fintecher.pangolin.common.enums.CaseLeaveFlag;
import cn.fintecher.pangolin.entity.elastic.BaseCase;
import cn.fintecher.pangolin.entity.elastic.LeaveCaseModel;
import cn.fintecher.pangolin.entity.elastic.StopCaseModel;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import cn.fintecher.pangolin.service.dataimp.elastic.BaseCaseElastic;
import cn.fintecher.pangolin.service.dataimp.model.ConfirmCommonModel;
import cn.fintecher.pangolin.service.dataimp.repository.CaseInfoRepository;
import cn.fintecher.pangolin.common.utils.RepositoryUtil;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 16:52 2019/11/5
 */

@Service("processCaseFuture")
public class ProcessCaseFuture {
    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Autowired
    private BaseCaseElastic baseCaseElastic;

    /**
     * 处理案件
     */
    @Async
    public CompletableFuture<List<CaseInfo>> processCaseInfo(List<LeaveCaseModel> models, List<StopCaseModel> stopCaseModels, int i) {
        List<CaseInfo> list = new ArrayList<>();
        List<String> caseNumbers = new ArrayList<>();
        Iterable<CaseInfo> iterable;
        if (i == 0) { //留案
            for (LeaveCaseModel model : models) {
                caseNumbers.add(model.getCaseNumber());
            }
            iterable = caseInfoRepository.findAll(QCaseInfo.caseInfo.caseNumber.in(caseNumbers));
            List<CaseInfo> caseInfos = IterableUtils.toList(iterable);
            BoolQueryBuilder builder = new BoolQueryBuilder();
            builder.must(QueryBuilders.termsQuery("caseNumber.keyword", caseNumbers));
            Iterable<BaseCase> baseCaseIterable = baseCaseElastic.search(builder);
            List<BaseCase> baseCases = IterableUtils.toList(baseCaseIterable);
            Map<String, ConfirmCommonModel> map = new HashMap<>();
            for (LeaveCaseModel model : models) {
                ConfirmCommonModel confirmCommonModel = new ConfirmCommonModel();
                confirmCommonModel.setLeaveCaseModel(model);
                for (CaseInfo caseInfo : caseInfos) {
                    if (model.getCaseNumber().equals(caseInfo.getCaseNumber())) {
                        confirmCommonModel.setCaseInfo(caseInfo);
                        break;
                    }
                }
                for (BaseCase baseCase : baseCases) {
                    if (model.getCaseNumber().equals(baseCase.getCaseNumber())) {
                        confirmCommonModel.setBaseCase(baseCase);
                        break;
                    }
                }
                map.put(model.getCaseNumber(), confirmCommonModel);
            }
            List<BaseCase> list1 = new ArrayList<>();
            for (Map.Entry<String, ConfirmCommonModel> entry : map.entrySet()) {
                ConfirmCommonModel confirmCommonModel = entry.getValue();
                CaseInfo caseInfo = confirmCommonModel.getCaseInfo();
                BaseCase baseCase = confirmCommonModel.getBaseCase();
                LeaveCaseModel leaveCaseModel = confirmCommonModel.getLeaveCaseModel();
                if (Objects.nonNull(caseInfo)) {
                    caseInfo.setEndCaseDate(leaveCaseModel.getOriginalDate());
                    caseInfo.setLeaveFlag(CaseLeaveFlag.NO_LEAVE);
                    caseInfo.setLeaveExpireDate(null);
                    list.add(caseInfo);
                }
                if (Objects.nonNull(baseCase)) {
                    baseCase.setEndCaseDate(leaveCaseModel.getOriginalDate());
                    list1.add(baseCase);
                }
            }
            RepositoryUtil.saveData(baseCaseElastic, list1);
        } else { //停催
            for (StopCaseModel model : stopCaseModels) {
                caseNumbers.add(model.getCaseNumber());
            }
            iterable = caseInfoRepository.findAll(QCaseInfo.caseInfo.caseNumber.in(caseNumbers));
            List<CaseInfo> caseInfos = IterableUtils.toList(iterable);
            for (CaseInfo caseInfo : caseInfos) {
                if (Objects.isNull(caseInfo.getDepartment()) && Objects.nonNull(caseInfo.getCurrentCollector())) {
                    caseInfo.setCaseDataStatus(CaseDataStatus.IN_POOL);
                }
                caseInfo.setStopTime(null);
                list.add(caseInfo);
            }
        }
        return CompletableFuture.completedFuture(list);
    }
}
